/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.conn;

import aptech.semester.code.common.Constants;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class MyLib {
    public static Connection getCN() {
        Connection cn = null;
        Properties prop = new Properties();
    	InputStream input = null;
        try {
            String filename = Constants.PATH_FOLDER_CONFIG + Constants.NAME_FILE_CONFIG;
            File f = new File(filename);
            input = new FileInputStream(f);
            
            if (input == null) {
                System.out.println("Sorry, unable to find " + filename);
                
            } else {
                prop.load(input);
            
                String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                String userName = prop.getProperty("username");
                String password = prop.getProperty("password");
                String port = prop.getProperty("port");
                String databaseName = prop.getProperty("database_name");
                String serverName = prop.getProperty("server_name");
                String url = "jdbc:sqlserver://" + serverName + ":" + port+";database="+databaseName;
                //1. load driver sql loai 4
                Class.forName(driverName);

                //2. tao ket noi dem csdl : java_dbDemo
                //String url = "jdbc:sqlserver://localhost:1433;database=MINI_HRM;";
                cn = DriverManager.getConnection(url,userName,password);
            }
        } catch (ClassNotFoundException e) {
            
        } catch(SQLException e) {
            
        } catch(IOException e) {
            
        }
        return cn;
    }
}

